package com.qipay.security;

import com.qipay.baas.model.User;
import com.qipay.mapper.UserMapper;
import com.qipay.user.entity.UserLevel;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.util.Assert;

import java.util.Collection;


public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    UserMapper userRepository;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        Assert.notNull(StringUtils.trimToNull(userName), "user name must be import!");


        User user = userRepository.findFirstByName(userName);
        if (user == null) {
            throw new UsernameNotFoundException(String.format("User %s does not exist!", userName));
        }
//            if (StringUtils.isBlank(user.getHeadPic())) {
//                user.setHeadPic(resourceService.getDefaultAvatar());
//            }
        UserLevel userLevel = UserLevel.CP;
        if (user.getLevel() == 200) {
            userLevel = UserLevel.AGENT;
        }
        if (user.getLevel() == 1000 || user.getLevel() == 500) {
            userLevel = UserLevel.ADMIN;
        }
        return new LoginUserDetails(user.getId(), user.getName(), user.getPwd(), userLevel, user.getPid());

//            if (StringUtils.isNumeric(username)) {
//                User user = userRepository.findOne(Long.parseLong(username));
//                if (user == null) {
//                    throw new UsernameNotFoundException(String.format("User %s does not exist!", username));
//                }
//                if (StringUtils.isBlank(user.getHeadPic())) {
//                    user.setHeadPic(resourceService.getDefaultAvatar());
//                }
//                return new LoginUserDetails(user.getId(), user.getNickName(), user.getPassword(), user.getNickName(), user.getHeadPic());
//            }
    }

    /**
     * 继承的是楼上UserDetails噢噢，所以部分参数初始化也是上面哦
     */
    public static class LoginUserDetails implements UserDetails {

        /*
        *后台的用户Id
         */
        private int id;

        /*
        * 登录名，一般为CpId
         */
        private String username;


        private String password;

        private String dbPassword;

        private UserLevel level;
        private long pid;

        private static final long serialVersionUID = -5958906296908027297L;

        public LoginUserDetails(int id, String username, String dbPassword, UserLevel level, long pid) {
            this.id = id;
            this.username = username;
            this.dbPassword = dbPassword;

            this.password = DigestUtils.md5Hex(dbPassword);
            this.level = level;
            this.pid = pid;
        }

        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            return null;
        }

        @Override
        public String getPassword() {
            return this.password;
        }

        @Override
        public String getUsername() {
            return this.username;
        }

        @Override
        public boolean isAccountNonExpired() {
            return true;
        }

        @Override
        public boolean isAccountNonLocked() {
            return true;
        }

        @Override
        public boolean isCredentialsNonExpired() {
            return true;
        }

        @Override
        public boolean isEnabled() {
            return true;
        }

        public int getId() {
            return this.id;
        }

        public void setId(int id) {
            this.id = id;
        }


        public String getDbPassword() {
            return dbPassword;
        }

        public void setDbPassword(String dbPassword) {
            this.dbPassword = dbPassword;
        }

        public UserLevel getLevel() {
            return this.level;
        }

        public long getPid() {
            return this.pid;
        }

        public long getCpId() {
            if (level.getCode() < 500) {
                return Long.valueOf(username);
            }
            return 0L;
        }
    }
}
